package com.qf.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.user.dao.UserDao;
import com.qf.user.entity.User;
import com.qf.user.service.UserService;
import com.qf.vo.PageDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public PageDataVo list(User user, Integer current, Integer size) {

        Page<User> userPage = new Page<>(current, size);

        // 如何处理查询条件呢？
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq(user.getName() != null, "name", user.getName())
                .eq(user.getAge() != null, "age", user.getAge())
                .eq(user.getEmail() != null, "email", user.getEmail());

//        if (user.getName()!=null){
//            queryWrapper.eq("name",user.getName());
//        }
//
//        if (user.getAge()!=null){
//            queryWrapper.eq("age",user.getAge());
//        }
//
//        if (user.getEmail()!=null){
//            queryWrapper.eq("email",user.getEmail());
//        }

        userDao.selectPage(userPage, queryWrapper);

        long total = userPage.getTotal();
        List<User> records = userPage.getRecords();

        return new PageDataVo(total, records);

    }
}
